// pages/mine/bill/index.js
const { http } = require("../../../utils/request");
let utils = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    startDate: "",
    endDate: "",
    totalData: null,
    billData: null,
    pageIndex: 1,
    isLoad: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let date = utils.formatDate(new Date());
    let dateSub = date.split("-");
    this.setData({
      startDate: dateSub[0] + "-" + dateSub[1] + "-01",
      endDate: date
    })
    this.queryBillTotal();
    this.queryBillData();
  },

  /** 账单汇总 */
  queryBillTotal(){
    let that = this;
    wx.showLoading();
    http.requestData("GET","RefundMoney/GetApiListSum",{
      s: this.data.startDate,
      e: this.data.endDate
    }).then(res=>{
      wx.hideLoading();
      if(res.Tag == 10){
        that.setData({
          totalData: res.Data
        })
      }else{
        http.showToast(res.Message);
      }
    }).catch(e=>{
      wx.hideLoading();
    })
  },

  /** 账单列表 */
  queryBillData(){
    let that = this;
    http.requestData("GET","RefundMoney/GetApiPageList",{
      s: this.data.startDate,
      e: this.data.endDate,
      page: this.data.pageIndex,
      pageSize: 10
    }).then(res=>{
      if(res.Tag == 10){
        if(that.data.pageIndex==1){
          that.setData({
            billData: []
          })
        }
        if(res.Data && res.Data.length>0){
          res.Data.forEach(item => {
            that.data.billData.push(item);
          });
        }
        that.setData({
          billData: that.data.billData,
          isLoad: res.Data.length<10?true:false
        })
      }else{
        http.showToast(res.Message);
      }
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  SbindDateChange(e){
    this.setData({
      startDate: e.detail.value
    })
  },

  EbindDateChange(e){
    this.setData({
      endDate: e.detail.value
    })
  },

  searchTap(){
    this.setData({
      pageIndex: 1,
      isLoad: false
    })
    this.queryBillTotal();
    this.queryBillData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.isLoad){
      return;
    }
    this.setData({
      pageIndex: this.data.pageIndex + 1
    })
    this.queryBillData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})